package cn.edu.hit;

import cn.edu.hit.core.Document;
import cn.edu.hit.core.db.*;
import cn.edu.hit.kg.data.GraphData;
import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GetPortandName {
    static DataConnection connection = DataConnectionLoader.getInstance();
    static DataReader reader = connection.getReader();
    static DBReadHelper dbHelper = new DBReadHelper(reader);
    static GraphData graphData = GraphData.getInstance();

    public static List<Map<String, Object>> listFaults() {
        return graphData.getByLabel("Fault")
                .filter(node -> node.asMap().containsKey("outId") && node.get("outId").toString().length() >= 7)
                .map(node -> {
                    Map<String, Object> nodeMap = new HashMap<>();
                    nodeMap.put("identity", node.id());
                    nodeMap.put("labels", node.labels());
                    nodeMap.put("properties", node.asMap());
                    nodeMap.put("ports", getFaultPort(node.id()));
                    return nodeMap;
                }).collect(Collectors.toList());
    }

    public static List<Map<String, String>> getFaultPort(Long faultId) {
        List<String> featureList = graphData.getAllSubNodes(faultId)
                .filter(node -> node.hasLabel("Feature"))
                .map(node -> (String) node.asMap().get("name"))
                .collect(Collectors.toList());

//        List<String> portNames = new ArrayList<>();
        List<Map<String, String>> ports = new ArrayList<>();
        for (int ij = 0; ij < featureList.size(); ij++) {
            String featureName = featureList.get(ij);
            List<Document> channelList = reader.queryByTemplate(Filters.eq("signalType", featureName));
            for (int j = 0; j < channelList.size(); j++) {
                String channelIdStr = channelList.get(j).getString("CombineChanenels");
                JSONArray idArray = JSONArray.fromObject(channelIdStr);
//                String[] IdArray = channelIdStr.substring(1, channelIdStr.length() - 1).split(",");
                for (int k = 0; k < idArray.size(); k++) {
                    String portId = idArray.getString(k);
                    Document portDocunment = dbHelper.getById(portId);
                    if (portDocunment != null) {
                        String dataSourceArgs = portDocunment.getString("dataSourceArgs");
                        String portName = portDocunment.getString("portName");
                        Map<String, String> portMap = new HashMap<>();
                        portMap.put(portName, dataSourceArgs);
                        boolean hasport = false;
                        for (int l = 0; l < ports.size(); l++) {
                            if (ports.get(l).equals(portMap)) {
                                hasport = true;
                            }
                        }
                        if (!hasport) {
                            ports.add(portMap);
                        }
                    }
                }
            }
        }
        return ports;
    }


}
